#include "Scheme/GUIWidgetLookManager.h"
#include "GUIBase.h"
#include "GUISystem.h"
#include "XML/GUIXMLWidgetLookHandler.h"

namespace YGUI
{
    template<>
    WidgetLookManager* Singleton<WidgetLookManager>::m_singleton = 0;

    WidgetLookManager::WidgetLookManager(void)
    {
    }

    WidgetLookManager::~WidgetLookManager(void)
    {
    }

    WidgetLookManager& WidgetLookManager::getSingleton(void)
    {
        static WidgetLookManager instance;
        return *m_singleton;
    }

    WidgetLookManager* WidgetLookManager::getSingletonPtr(void)
    {
        if(m_singleton == NULL)
            getSingleton();
        return m_singleton;
    }

    const WidgetLook& WidgetLookManager::getWidgetLook(const String& name) const
    {
        WidgetLookList::const_iterator it = m_widgets.find(name);
        if(it == m_widgets.end())
            throw Exception("Specified WidgetLook is not found: %s", name.c_str());
        else return it->second;
    }

    bool WidgetLookManager::isWidgetLookExists(const String& name) const
    {
        return (m_widgets.find(name) != m_widgets.end());
    }

    void WidgetLookManager::addWidgetLook(const WidgetLook& look)
    {
        if(isWidgetLookExists(look.getName()))
        {
            return;
        }
        else m_widgets[look.getName()] = look;
    }

    void WidgetLookManager::removeWidgetLook(const String& name)
    {
        WidgetLookList::iterator it = m_widgets.find(name);
        if(it != m_widgets.end())
        {
            m_widgets.erase(it);
        }
    }

    void WidgetLookManager::parseLookNFeelFile(const String& filename)
    {
        WidgetLookXMLHandler handler(this);
        System::getSingleton().getXMLParser()->parseXMLFile(handler, filename);
    }
};